import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// 点按钮切换图片
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String imgName = '33.jpg';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Material App Bar'),
        ),
        body: Center(
          child: Column(
            children: [
              // 图片
              ImageShow(imgName: imgName),
              // 按钮
              ElevatedButton(
                onPressed: () {
                  if (imgName == '33.jpg') {
                    imgName = '44.jpeg';
                  } else {
                    imgName = '33.jpg';
                  }
                  setState(() {});
                },
                child: const Text('换一换'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class ImageShow extends StatelessWidget {
  final String imgName;

  const ImageShow({super.key, required this.imgName});

  @override
  Widget build(BuildContext context) {
    return Image.asset('assets/$imgName');
  }
}
